<?php
/**
 * 后台管理基础类
 * Created by PhpStorm.
 * User: deng
 * Date: 2016/4/26
 * Time: 12:15
 */
class AdminBase {

    protected $req;
    protected $op;

    function __construct(){
        $this->req = request();
        $this->op = $this->filters($this->req['op'],'index');
        $this->op .= 'Action';
    }

    function fetchData($param){
        global $_SC;
        $content = get_api_content($_SC['apiurl'], $param);
        $data = json_decode($content,true);
//        if (empty($data) || !empty($data['errorNo'])){
//            $this->errorReturn($data['resultData'],$data['errorNo']);
//        }
		 $code = empty($data['errorNo']) ? 1: $data['errorNo'];
        $this->jsonReturn($data['resultData'],$code);
    }

    protected function jsonReturn($result = [], $code = 1, $msg = '操作成功'){
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode([
            '_code' => $code,
            '_msg' => $msg,
            'result' => $result
        ]);
        exit();
    }

    protected function resultReturn($result = []){
        $this->jsonReturn($result);
    }

    protected function errorReturn($msg = '', $code = 0){
        empty($msg) && $msg = '操作失败' ;
        $this->jsonReturn([], $code, $msg);
    }

    function __call($name, $arguments)
    {
        $msg = __FILE__ . '文件方法' . $this->op . '不存在';
        $this->errorReturn($msg);
    }


    protected function filters($reqParam,$default='')
    {
       return  (isset($reqParam) && !empty($reqParam) )
            ?
            trim($reqParam)
            :
            $default;
    }

    protected function urlencodeReq(&$req)
    {
        foreach ($req as &$item){
            if(is_array($item)) {
                $this->urlencodeReq($item);
                continue;
            }
            $item = urlencode($item);
        }
    }
}